<html><head><title>CanvasGadget.pb</title></head>
<body bgcolor="#FFFFDF" link="#009999" vlink="#006666" alink="#006666">
<pre><font face="Courier New, Courier, mono"size="2"><font color="#006666">;
</font><font color="#006666">; ------------------------------------------------------------
</font><font color="#006666">;
</font><font color="#006666">;   PureBasic - CanvasGadget example file
</font><font color="#006666">;
</font><font color="#006666">;    (c) Fantaisie Software
</font><font color="#006666">;
</font><font color="#006666">; ------------------------------------------------------------
</font><font color="#006666">;
</font>
<b><font color="#006666">Enumeration</font></b>
  <font color="#924B72">#IMAGE_Content</font>  <font color="#006666">; stores the previous CanvasGadget content while the mouse is down
</font>  <font color="#924B72">#IMAGE_Color</font>
  <font color="#924B72">#IMAGE_LoadSave</font> 
<b><font color="#006666">EndEnumeration</font></b>

<b><font color="#006666">Enumeration</font></b>
  <font color="#924B72">#GADGET_Canvas</font>
  <font color="#924B72">#GADGET_Color</font> 
  <font color="#924B72">#GADGET_Brush</font>
  <font color="#924B72">#GADGET_Line</font>
  <font color="#924B72">#GADGET_Box</font>
  <font color="#924B72">#GADGET_Circle</font>
  <font color="#924B72">#GADGET_Fill</font>
  <font color="#924B72">#GADGET_Clear</font> 
  <font color="#924B72">#GADGET_Load</font>
  <font color="#924B72">#GADGET_Save</font>
<b><font color="#006666">EndEnumeration</font></b>

<b><font color="#006666">Global</font></b> CurrentColor, CurrentMode, StartX, StartY

<font color="#006666">; Draw the mouse action result depending on the currently selected mode and event type
</font><font color="#006666">;
</font><b><font color="#006666">Procedure</font></b> <font color="#006666">DrawAction</font>(x, y, EventType)

  <b><font color="#006666">If</font></b> <font color="#006666">StartDrawing</font>(<font color="#006666">CanvasOutput</font>(<font color="#924B72">#GADGET_Canvas</font>))
    <b><font color="#006666">Select</font></b> CurrentMode
    
      <b><font color="#006666">Case</font></b> <font color="#924B72">#GADGET_Brush</font>
        <b><font color="#006666">If</font></b> EventType = <font color="#924B72">#PB_EventType_LeftButtonDown</font> <b><font color="#006666">Or</font></b> EventType = <font color="#924B72">#PB_EventType_MouseMove</font>
<font color="#006666">          Circle</font>(x, y, 5, CurrentColor)
        <b><font color="#006666">EndIf</font></b>
        
      <b><font color="#006666">Case</font></b> <font color="#924B72">#GADGET_Line</font>
<font color="#006666">        DrawImage</font>(<font color="#006666">ImageID</font>(<font color="#924B72">#IMAGE_Content</font>), 0, 0)
<font color="#006666">        LineXY</font>(StartX, StartY, x, y, CurrentColor)
      
      <b><font color="#006666">Case</font></b> <font color="#924B72">#GADGET_Box</font>
<font color="#006666">        DrawImage</font>(<font color="#006666">ImageID</font>(<font color="#924B72">#IMAGE_Content</font>), 0, 0)
<font color="#006666">        Box</font>(StartX, StartY, x-StartX, y-StartY, CurrentColor)
        
      <b><font color="#006666">Case</font></b> <font color="#924B72">#GADGET_Circle</font>
<font color="#006666">        DrawImage</font>(<font color="#006666">ImageID</font>(<font color="#924B72">#IMAGE_Content</font>), 0, 0)
        
        <b><font color="#006666">If</font></b> x &#062; StartX
          rx = x - StartX
        <b><font color="#006666">Else</font></b>
          rx = StartX - x
        <b><font color="#006666">EndIf</font></b>
        
        <b><font color="#006666">If</font></b> y &#062; StartY
          ry = y - StartY
        <b><font color="#006666">Else</font></b>
          ry = StartY - y
        <b><font color="#006666">EndIf</font></b>
        
        <b><font color="#006666">If</font></b> <font color="#006666">GetGadgetAttribute</font>(<font color="#924B72">#GADGET_Canvas</font>, <font color="#924B72">#PB_Canvas_Modifiers</font>) & <font color="#924B72">#PB_Canvas_Control</font>
          ry = rx
        <b><font color="#006666">EndIf</font></b>
        
<font color="#006666">        Ellipse</font>(StartX, StartY, rx, ry, CurrentColor)
      
      <b><font color="#006666">Case</font></b> <font color="#924B72">#GADGET_Fill</font>
        <b><font color="#006666">If</font></b> EventType = <font color="#924B72">#PB_EventType_LeftButtonDown</font>
<font color="#006666">          FillArea</font>(x, y, -1, CurrentColor)
        <b><font color="#006666">EndIf</font></b>
  
    <b><font color="#006666">EndSelect</font></b>
    
<font color="#006666">    StopDrawing</font>()
  <b><font color="#006666">EndIf</font></b>

<b><font color="#006666">EndProcedure</font></b>

<font color="#006666">UseJPEGImageDecoder</font>()
<font color="#006666">UseJPEGImageEncoder</font>()

CurrentColor = $000000
CurrentMode  = <font color="#924B72">#GADGET_Brush</font>
<font color="#006666">CreateImage</font>(<font color="#924B72">#IMAGE_Color</font>, 35, 35, 24)
<font color="#006666">CreateImage</font>(<font color="#924B72">#IMAGE_Content</font>, 380, 380, 24)

<b><font color="#006666">If</font></b> <font color="#006666">OpenWindow</font>(0, 0, 0, 460, 400, "CanvasGadget", <font color="#924B72">#PB_Window_SystemMenu</font> | <font color="#924B72">#PB_Window_ScreenCentered</font>)
<font color="#006666">  CanvasGadget</font>(<font color="#924B72">#GADGET_Canvas</font>, 10, 10, 380, 380, <font color="#924B72">#PB_Canvas_ClipMouse</font>)

<font color="#006666">  ButtonImageGadget</font>(<font color="#924B72">#GADGET_Color</font>, 400, 10, 50, 50,<font color="#006666"> ImageID</font>(<font color="#924B72">#IMAGE_Color</font>))
  
<font color="#006666">  ButtonGadget</font>(<font color="#924B72">#GADGET_Brush</font>,  400, 100, 50, 25, "Brush",  <font color="#924B72">#PB_Button_Toggle</font>)
<font color="#006666">  ButtonGadget</font>(<font color="#924B72">#GADGET_Line</font>,   400, 130, 50, 25, "Line",   <font color="#924B72">#PB_Button_Toggle</font>)
<font color="#006666">  ButtonGadget</font>(<font color="#924B72">#GADGET_Box</font>,    400, 160, 50, 25, "Box",    <font color="#924B72">#PB_Button_Toggle</font>)
<font color="#006666">  ButtonGadget</font>(<font color="#924B72">#GADGET_Circle</font>, 400, 190, 50, 25, "Circle", <font color="#924B72">#PB_Button_Toggle</font>)
<font color="#006666">  ButtonGadget</font>(<font color="#924B72">#GADGET_Fill</font>,   400, 220, 50, 25, "Fill",   <font color="#924B72">#PB_Button_Toggle</font>)
    
<font color="#006666">  ButtonGadget</font>(<font color="#924B72">#GADGET_Clear</font>,  400, 280, 50, 25, "Clear")
  
<font color="#006666">  ButtonGadget</font>(<font color="#924B72">#GADGET_Load</font>,   400, 335, 50, 25, "Load")
<font color="#006666">  ButtonGadget</font>(<font color="#924B72">#GADGET_Save</font>,   400, 365, 50, 25, "Save")
  
<font color="#006666">  SetGadgetState</font>(<font color="#924B72">#GADGET_Brush</font>, 1)
<font color="#006666">  SetGadgetAttribute</font>(<font color="#924B72">#GADGET_Canvas</font>, <font color="#924B72">#PB_Canvas_Cursor</font>, <font color="#924B72">#PB_Cursor_Cross</font>)

  <b><font color="#006666">Repeat</font></b>
    Event =<font color="#006666"> WaitWindowEvent</font>()
    
    <b><font color="#006666">If</font></b> Event = <font color="#924B72">#PB_Event_Gadget</font>
    
      <b><font color="#006666">Select</font></b> <font color="#006666">EventGadget</font>()
      
        <b><font color="#006666">Case</font></b> <font color="#924B72">#GADGET_Canvas</font>
          X =<font color="#006666"> GetGadgetAttribute</font>(<font color="#924B72">#GADGET_Canvas</font>, <font color="#924B72">#PB_Canvas_MouseX</font>)
          Y =<font color="#006666"> GetGadgetAttribute</font>(<font color="#924B72">#GADGET_Canvas</font>, <font color="#924B72">#PB_Canvas_MouseY</font>)
          Type =<font color="#006666"> EventType</font>()
        
          <b><font color="#006666">Select</font></b> <font color="#006666">EventType</font>()
          
            <b><font color="#006666">Case</font></b> <font color="#924B72">#PB_EventType_LeftButtonDown</font>
              <font color="#006666">;
</font>              <font color="#006666">; This stores the current content of the CanvasGadget in #IMAGE_Content,
</font>              <font color="#006666">; so it can be re-drawn while the mouse moves
</font>              <font color="#006666">;
</font>              <b><font color="#006666">If</font></b> <font color="#006666">StartDrawing</font>(<font color="#006666">ImageOutput</font>(<font color="#924B72">#IMAGE_Content</font>))
<font color="#006666">                DrawImage</font>(<font color="#006666">GetGadgetAttribute</font>(<font color="#924B72">#GADGET_Canvas</font>, <font color="#924B72">#PB_Canvas_Image</font>), 0, 0)
<font color="#006666">                StopDrawing</font>()
              <b><font color="#006666">EndIf</font></b>
              
              StartX = X
              StartY = Y
<font color="#006666">              DrawAction</font>(X, Y,<font color="#006666"> EventType</font>())

            
            <b><font color="#006666">Case</font></b> <font color="#924B72">#PB_EventType_LeftButtonUp</font>
<font color="#006666">              DrawAction</font>(X, Y,<font color="#006666"> EventType</font>())                        
            
            <b><font color="#006666">Case</font></b> <font color="#924B72">#PB_EventType_MouseMove</font>
              <b><font color="#006666">If</font></b> <font color="#006666">GetGadgetAttribute</font>(<font color="#924B72">#GADGET_Canvas</font>, <font color="#924B72">#PB_Canvas_Buttons</font>) & <font color="#924B72">#PB_Canvas_LeftButton</font>
<font color="#006666">                DrawAction</font>(X, Y,<font color="#006666"> EventType</font>())            
              <b><font color="#006666">EndIf</font></b>
                      
          <b><font color="#006666">EndSelect</font></b>
        
        <b><font color="#006666">Case</font></b> <font color="#924B72">#GADGET_Color</font>
          CurrentColor =<font color="#006666"> ColorRequester</font>(CurrentColor)
          <b><font color="#006666">If</font></b> <font color="#006666">StartDrawing</font>(<font color="#006666">ImageOutput</font>(<font color="#924B72">#IMAGE_Color</font>))
<font color="#006666">            Box</font>(0, 0, 35, 35, CurrentColor)
<font color="#006666">            StopDrawing</font>()
<font color="#006666">            SetGadgetAttribute</font>(<font color="#924B72">#GADGET_Color</font>, <font color="#924B72">#PB_Button_Image</font>,<font color="#006666"> ImageID</font>(<font color="#924B72">#IMAGE_Color</font>))
          <b><font color="#006666">EndIf</font></b>
          
        <b><font color="#006666">Case</font></b> <font color="#924B72">#GADGET_Brush</font>, <font color="#924B72">#GADGET_Line</font>, <font color="#924B72">#GADGET_Box</font>, <font color="#924B72">#GADGET_Circle</font>, <font color="#924B72">#GADGET_Fill</font>
          EventGadget =<font color="#006666"> EventGadget</font>()
          <b><font color="#006666">For</font></b> Gadget = <font color="#924B72">#GADGET_Brush</font> <b><font color="#006666">To</font></b> <font color="#924B72">#GADGET_Fill</font>
            <b><font color="#006666">If</font></b> Gadget = EventGadget
<font color="#006666">              SetGadgetState</font>(Gadget, 1) 
            <b><font color="#006666">Else</font></b>
<font color="#006666">              SetGadgetState</font>(Gadget, 0) <font color="#006666">; unset the state of all other gadgets
</font>            <b><font color="#006666">EndIf</font></b>
          <b><font color="#006666">Next</font></b> Gadget          
          CurrentMode = EventGadget             
      
        <b><font color="#006666">Case</font></b> <font color="#924B72">#GADGET_Clear</font>
          <b><font color="#006666">If</font></b> <font color="#006666">StartDrawing</font>(<font color="#006666">CanvasOutput</font>(<font color="#924B72">#GADGET_Canvas</font>))
<font color="#006666">            Box</font>(0, 0, 380, 380, $FFFFFF)
<font color="#006666">            StopDrawing</font>()
          <b><font color="#006666">EndIf</font></b>
      
        <b><font color="#006666">Case</font></b> <font color="#924B72">#GADGET_Load</font>
          File$ =<font color="#006666"> OpenFileRequester</font>("Load Image...", "", "JPEG Images|*.jpeg|All Files|*.*", 0)
          <b><font color="#006666">If</font></b> File$
            <b><font color="#006666">If</font></b> <font color="#006666">LoadImage</font>(<font color="#924B72">#IMAGE_LoadSave</font>, File$)
              <b><font color="#006666">If</font></b> <font color="#006666">StartDrawing</font>(<font color="#006666">CanvasOutput</font>(<font color="#924B72">#GADGET_Canvas</font>))
<font color="#006666">                Box</font>(0, 0, 380, 380, $FFFFFF)
<font color="#006666">                DrawImage</font>(<font color="#006666">ImageID</font>(<font color="#924B72">#IMAGE_LoadSave</font>), 0, 0)
<font color="#006666">                StopDrawing</font>()
              <b><font color="#006666">EndIf</font></b>
<font color="#006666">              FreeImage</font>(<font color="#924B72">#IMAGE_LoadSave</font>)
            <b><font color="#006666">Else</font></b>
<font color="#006666">              MessageRequester</font>("CanvasGadget", "Cannot load image: " + File$)
            <b><font color="#006666">EndIf</font></b>
          <b><font color="#006666">EndIf</font></b>
              
        <b><font color="#006666">Case</font></b> <font color="#924B72">#GADGET_Save</font>
          File$ =<font color="#006666"> SaveFileRequester</font>("Save Image...", File$, "JPEG Images|*.jpeg|All Files|*.*", 0)
          <b><font color="#006666">If</font></b> File$ <b><font color="#006666">And</font></b> (<font color="#006666">FileSize</font>(File$) = -1 <b><font color="#006666">Or</font></b> <font color="#006666">MessageRequester</font>("CanvasGadget", "Overwrite this file? " + File$, <font color="#924B72">#PB_MessageRequester_YesNo</font>) = <font color="#924B72">#PB_MessageRequester_Yes</font>)
            <b><font color="#006666">If</font></b> <font color="#006666">CreateImage</font>(<font color="#924B72">#IMAGE_LoadSave</font>, 380, 380, 24) <b><font color="#006666">And</font></b> <font color="#006666">StartDrawing</font>(<font color="#006666">ImageOutput</font>(<font color="#924B72">#IMAGE_LoadSave</font>))
<font color="#006666">              DrawImage</font>(<font color="#006666">GetGadgetAttribute</font>(<font color="#924B72">#GADGET_Canvas</font>, <font color="#924B72">#PB_Canvas_Image</font>), 0, 0)
<font color="#006666">              StopDrawing</font>()
              
              <b><font color="#006666">If</font></b> <font color="#006666">SaveImage</font>(<font color="#924B72">#IMAGE_LoadSave</font>, File$, <font color="#924B72">#PB_ImagePlugin_JPEG</font>) = 0
<font color="#006666">                MessageRequester</font>("CanvasGadget", "Cannot save image: " + File$)
              <b><font color="#006666">EndIf</font></b>
              
<font color="#006666">              FreeImage</font>(<font color="#924B72">#IMAGE_LoadSave</font>)
            <b><font color="#006666">EndIf</font></b>            
          <b><font color="#006666">EndIf</font></b>
                
      <b><font color="#006666">EndSelect</font></b>
    
    <b><font color="#006666">EndIf</font></b>
    
  <b><font color="#006666">Until</font></b> Event = <font color="#924B72">#PB_Event_CloseWindow</font>

<b><font color="#006666">EndIf</font></b>


</font></pre>
</body></html>
